package com.lww.smartbathroom.control;

import com.lww.smartbathroom.model.Equipment;
import com.lww.smartbathroom.model.EquipmentStatus;
import com.lww.smartbathroom.model.Result;
import com.lww.smartbathroom.param.EquipmentQueryParam;
import com.lww.smartbathroom.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;

/**
 * @author 刘正星
 * @version 1.0.0
 * @ClassName EquipmentController.java
 * @Description TODO
 * @createTime 2021年01月24日 16:17:00
 */
@RestController
@RequestMapping("/equipment")
public class EquipmentController {
    @Autowired
    private EquipmentService equipmentService;

// @PostConstruct
    public void init(){
        for (int i = 0; i <=100 ; i++) {
            Equipment equipment = new Equipment();
            equipment.setIndex(String.valueOf(i));
            equipment.setStatus(EquipmentStatus.NOT_USE);
                equipment.setLocation("男");

            equipmentService.add(equipment);
        }
     for (int i = 0; i <=100 ; i++) {
         Equipment equipment = new Equipment();
         equipment.setIndex(String.valueOf(i));
         equipment.setStatus(EquipmentStatus.NOT_USE);

         equipment.setLocation("女");
         equipmentService.add(equipment);
     }
    }

    @RequestMapping("/get")
    public Equipment get(@RequestParam("id") String id) {

        return equipmentService.get(id);

    }

    @PostMapping("/add")
    public Equipment add(@RequestBody Equipment equipment) {
        if (StringUtils.hasText(equipment.getId())){
            equipmentService.modify(equipment);
            return equipment;
        }else {
            return equipmentService.add(equipment);
        }

    }

    @RequestMapping("/list")
    public Result list(@RequestBody EquipmentQueryParam equipmentQueryParam) {

        Result result = new Result();
        if (equipmentQueryParam == null) {
            result.setMessage("param is null");
            result.setSuccess(false);
            return result;
        }
        Page<Equipment> equipmentPage = equipmentService.list(equipmentQueryParam);
        result.setSuccess(true);
        result.setData(equipmentPage);
        return result;
    }
    @RequestMapping("/listBySex")
    public Result listBySex(@RequestParam("page") Integer page,@RequestParam("limit")Integer limit,@RequestParam("sex")String sex) {
        EquipmentQueryParam equipmentQueryParam = new EquipmentQueryParam();
        equipmentQueryParam.setPageSize(limit);
        equipmentQueryParam.setPagination(page);
        if (sex.equals("female")){
            equipmentQueryParam.setLocation("男");
        }else{
            equipmentQueryParam.setLocation("女");
        }

        Result result = new Result();
        if (equipmentQueryParam == null) {
            result.setMessage("param is null");
            result.setSuccess(false);
            return result;
        }
        Page<Equipment> equipmentPage = equipmentService.list(equipmentQueryParam);
        result.setSuccess(true);
        result.setData(equipmentPage);
        return result;
    }
    @PostMapping("/modify")
    public boolean modifiy(@RequestBody Equipment equipment){
        return equipmentService.modify(equipment);
    }
    @RequestMapping("/del")
    public boolean del( String id){
        System.out.println(id);
        return equipmentService.delete(id);
    }

}
